package com.aoxin.service.impl.opm;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.aoxin.mapper.course.systemCourse.SchoolCoursePageDao;
import com.aoxin.mapper.opm.SlfClassroomMapper;
import com.aoxin.mapper.opm.SlfSchoolMapper;
import com.aoxin.model.dto.SelectDto;
import com.aoxin.model.opm.SlfSchool;
import com.aoxin.service.inter.opm.SchoolService;
import com.aoxin.util.ErrorUtil;
import com.aoxin.util.ThreadLocalUtil;

@Service("schoolService")
public class SchoolServiceImpl implements SchoolService {

	@Autowired
	private SlfSchoolMapper slfSchoolMapper;
	
	@Autowired
	private SlfClassroomMapper slfClassroomMapper;
	
	@Autowired
	private SchoolCoursePageDao schoolCoursePageDao;

	@Override
	@Transactional
	public Integer insertSlfSchool(SlfSchool school) {
		Assert.notNull(school, String.format(ErrorUtil.PARAMETER_NULL, "school"));
		school.setCreateDate(new Date());
		if (null == school.getSequence()) {
			school.setSequence(this.slfSchoolMapper.getNextSequence(school.getOrgId()).intValue());
		}
		if (this.slfSchoolMapper.checkName(school.getName(), null) > 0L) {
			throw new IllegalArgumentException(String.format(ErrorUtil.NAME_REPEAT, school.getName()));
		}
		this.slfSchoolMapper.insertSelective(school);
		return school.getSchoolId();
	}

	@Override
	@Transactional
	public void deleteSlfSchoolByIds(List<Integer> schoolIds) {
		Assert.notEmpty(schoolIds, String.format(ErrorUtil.PARAMETER_NULL, "schoolIds"));
		Map<String, Object> map = new HashMap<String, Object>();
		for (Integer integer : schoolIds) {
			map.put("schoolId", integer);
			if(this.slfClassroomMapper.getRecordCount(map) > 0L){
				throw new IllegalArgumentException(ErrorUtil.HAS_CLASSROOM);
			}
		}
		this.slfSchoolMapper.deleteByPrimaryKeys(schoolIds);
	}

	@Override
	@Transactional
	public void updateSlfSchool(SlfSchool school) {
		Assert.notNull(school, String.format(ErrorUtil.PARAMETER_NULL, "school"));
		if (this.slfSchoolMapper.checkName(school.getName(), school.getSchoolId()) > 0L) {
			throw new IllegalArgumentException(String.format(ErrorUtil.NAME_REPEAT, school.getName()));
		}
		String schoolName = slfSchoolMapper.getSchoolName(school.getSchoolId());	
		//判断是否绑定了教室
		Integer IsclassRoom = slfClassroomMapper.IsBindingclassroom(school.getSchoolId());
		if(IsclassRoom > 0){
		throw new IllegalArgumentException(String.format(ErrorUtil.BINDINGCLASSROOM, schoolName));
		}
		//判断是否绑定了包
		Integer IsPackage = schoolCoursePageDao.IsBindingPackage(school.getSchoolId());
		if(IsPackage > 0){
		throw new IllegalArgumentException(String.format(ErrorUtil.BINDINGPACKAGE, schoolName));
		}
		this.slfSchoolMapper.updateByPrimaryKeySelective(school);
	}

	@Override
	@Transactional
	public void updateSlfSchoolStates(List<Integer> schoolIds, Integer state) {
		Assert.notEmpty(schoolIds, String.format(ErrorUtil.PARAMETER_NULL, "schoolIds"));
		Assert.notNull(state, String.format(ErrorUtil.PARAMETER_NULL, "schoolId"));
		//判断教室里面
		if(state == 0){
		for(Integer schoolId : schoolIds){
		//找到shcoolName
			String schoolName = slfSchoolMapper.getSchoolName(schoolId);	
			//判断是否绑定了教室
			Integer IsclassRoom = slfClassroomMapper.IsBindingclassroom(schoolId);
			if(IsclassRoom > 0){
			throw new IllegalArgumentException(String.format(ErrorUtil.BINDINGCLASSROOM, schoolName));
			}
			//判断是否绑定了包
			Integer IsPackage = schoolCoursePageDao.IsBindingPackage(schoolId);
			if(IsPackage > 0){
			throw new IllegalArgumentException(String.format(ErrorUtil.BINDINGPACKAGE, schoolName));
			}
			}
		}
		this.slfSchoolMapper.updateStates(schoolIds, state);
	}

	@Override
	public SlfSchool getSlfSchool(Integer schoolId) {
		Assert.notNull(schoolId, String.format(ErrorUtil.PARAMETER_NULL, "schoolId"));
		return this.slfSchoolMapper.selectByPrimaryKey(schoolId);
	}

	@Override
	public List<SlfSchool> getSlfSchoolList(String name,Integer orgId,String addr,Integer state){
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("name",name);
		params.put("orgId",orgId);
		params.put("addr",addr);
		if(state == null){
		params.put("state",1);	
		}else{
		params.put("state",state);	
		}
		return this.slfSchoolMapper.getList(params);
	}

	@Override
	public Long getNextSequence(Integer parentId) {
		return this.slfSchoolMapper.getNextSequence(parentId);
	}

	@Override
	public List<SelectDto> getListByOrg() {
	//	Integer a = ThreadLocalUtil.getEmployee().getOrgId();
		return this.slfSchoolMapper.getListByOrg();
	}

	@Override
	public List<SelectDto> getSchoolSelectList() {
		return this.slfSchoolMapper.getSelectList();
	}
}
